package cn.itcast.service.system.impl;

import cn.itcast.dao.system.ModuleDao;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private ModuleDao moduleDao;

    @Override
    public List<Module> findAll() {
        return moduleDao.findAll();
    }

    @Override
    public void save(Module module) {
        moduleDao.save(module);
    }

    @Override
    public Module findById(String id) {
        return moduleDao.findById(id);
    }

    @Override
    public void update(Module module) {
        moduleDao.updateById(module);
    }

    @Override
    public void deleteById(String id) {
        moduleDao.deleteById(id);
    }

    @Override
    public PageInfo findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Module> list = moduleDao.findAll();
        return new PageInfo(list, 5);
    }

    @Override
    public List<String> findModulesByRoleId(String roleid) {

        return moduleDao.findModulesByRoleId(roleid);
    }

    @Override
    public List<Module> findModuleListByUser(User user) {
        //判断是那种人:SaaS管理员0  企业管理员1  普通员工
        Integer degree = user.getDegree();
        List<Module> moduleList = null;

        if (degree==0){
            moduleList = moduleDao.findByBelong(0);
        }else if (degree==1){
            moduleList = moduleDao.findModuleListByCompanyId(user.getCompanyId());
        }else {
            moduleList = moduleDao.findByUserId(user.getId());
        }

        return moduleList;
    }

    @Override
    public List<Module> findModuleListByCompanyId(String companyId) {
        return moduleDao.findModuleListByCompanyId(companyId);
    }


}
